package y2016.date1203.pm.古诗文网采集;

import y2016.date1203.pm.古诗文网采集.CaijiGo.CaiInfo;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainWindow extends JFrame implements ActionListener {

    /**
     *
     */
    private static final long serialVersionUID = -9128819609312354190L;

    public MainWindow() {
        this("main");
    }

    public MainWindow(String title) {
        setTitle(title);
        setSize(400, 600);
        setLayout(new java.awt.FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        setVisible(true);

    }

    /**
     * 链接
     */
    private JTextField jtf_url;
    /**
     * 路径
     */
    private JTextField jtf_path;
    /**
     * 替换文字
     */
    private JTextField jtf_deletes;
    private JTextField jtf_textFirst;
    private JTextField jtf_textLast;
    private JButton btn_ok;

    private CallBack callBack;

    public void callBack(CallBack callBack) {
        this.callBack = callBack;
    }

    private void init() {
        Box baseBox = Box.createVerticalBox();
        Box boxV1 = Box.createHorizontalBox();
        Box boxV2 = Box.createHorizontalBox();
        Box boxV3 = Box.createHorizontalBox();
        Box boxV4 = Box.createHorizontalBox();
        Box boxV5 = Box.createHorizontalBox();


        jtf_url = new JTextField();
        jtf_path = new JTextField("C:/Users/LuTao/Desktop/采集.txt");
        jtf_deletes = new JTextField();
        jtf_deletes.setText("作者：佚名");
        jtf_textFirst = new JTextField();
        jtf_textLast = new JTextField();
        btn_ok = new JButton("确定");
        jtf_loadText = new JLabel("删除文字可以是多组，以“|”区分");

        boxV1.add(new JLabel("下载链接："));
        boxV1.add(jtf_url);
        boxV1.add(Box.createVerticalStrut(40));

        boxV2.add(new JLabel("保存路径："));
        boxV2.add(jtf_path);
        boxV2.add(Box.createVerticalStrut(40));

        boxV3.add(new JLabel("删除文字："));
        boxV3.add(jtf_deletes);
        boxV3.add(Box.createVerticalStrut(40));

        boxV4.add(new JLabel("文前写入："));
        boxV4.add(jtf_textFirst);
        boxV4.add(Box.createVerticalStrut(40));

        boxV5.add(new JLabel("文后写入："));
        boxV5.add(jtf_textLast);
        boxV5.add(Box.createVerticalStrut(40));

        baseBox.add(boxV1);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(boxV2);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(boxV3);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(boxV4);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(boxV5);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(btn_ok);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(jtf_loadText);

        add(baseBox);

        btn_ok.addActionListener(this);
    }

    private JLabel jtf_loadText;

    public void setLoadText(String text) {
        jtf_loadText.setText(text);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getClass() + e.getActionCommand());
        String url = jtf_url.getText();
        String path = jtf_path.getText();
        String[] deletes = jtf_deletes.getText().split("|");
        String firstText = jtf_textFirst.getText();
        String lastText = jtf_textLast.getText();
        perforBack(new CaiInfo(path, url, deletes, firstText, lastText));

    }

    void perforBack(CaiInfo caiInfo) {
        if (callBack != null) {
            callBack.backInfo(caiInfo);
        }
    }

}

